package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.whmnx.base.bean.OrderDetailBean;
import com.whmnx.base.bean.WuLinBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.my.databinding.ActivityWuliBinding;
import com.whmnx.my.ui.activity.WuLiuActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * author : huang
 * date   : 2020/11/9 0009 11:56
 * version: 1.0
 */
public class WuLiModel extends ToolbarViewModel{
    public ObservableField<String> peoper = new ObservableField<>("");
    public ObservableField<String> phone = new ObservableField<>("");
    public ObservableField<String> address = new ObservableField<>("");
    public ObservableField<String> logisticsName = new ObservableField<>("");
    public ObservableField<String> courierNo = new ObservableField<>("");


    WuLiuActivity mActivity;
    ActivityWuliBinding mBinding;
    String order_id;

    public WuLiModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(WuLiuActivity wuLiuActivity, ActivityWuliBinding binding, String orderId) {
        this.mActivity = wuLiuActivity;
        this.mBinding = binding;
        this.order_id = orderId;
        setTitleText("填写物流");
      // peoper.set(SPUtils.getInstance().getString("rule_AfterSaleName"));
       // phone.set(SPUtils.getInstance().getString("rule_AfterSalePhone"));
      //  address.set(SPUtils.getInstance().getString("rule_AreaAddress") + SPUtils.getInstance().getString("rule_AreaAddressDesc"));
        Map<String, Object> params = new HashMap<>();
        List<String> list = new ArrayList<>();
        params.put("OrderNo",order_id);

        HttpClient.get(mActivity, Constants.LOGISTICS, params,new CallBack<WuLinBean>() {
            @Override
            public void onSuccess(WuLinBean result) {
                phone.set(result.getPhone());
                peoper.set(result.getName());
                address.set(result.getAddress());
            }
        });
    }


    //立即支付
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (TextUtils.isEmpty(logisticsName.get())){
                ToastUtils.showShort("请填写物流公司");
                return;
            }
            if (TextUtils.isEmpty(courierNo.get())){
                ToastUtils.showShort("请填写物流单号");
                return;
            }

            Map<String, Object> params = new HashMap<>();
            List<String> list = new ArrayList<>();
            params.put("order_OrderNo",order_id);
            params.put("backRemark","");
            params.put("order_State",16);
            params.put("imageModel",list);
            params.put("orderItem_BackRemark","");
            params.put("logisticsName",logisticsName.get());
            params.put("courierNo",courierNo.get());

            HttpClient.post(mActivity, Constants.BACKORDER, params,new CallBack<OrderDetailBean>() {
                @Override
                public void onSuccess(OrderDetailBean result) {
                    RxBus.getDefault().post(EventConstants.REQUEST);
                    finish();
                }
            });
        }
    });
}
